/*
 * @Description:
 * @Author: liyuhao
 * @Date: 2021-06-22 17:41:31
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2024-05-27 16:02:46
 */
import React, {useReducer} from "react";
import "./index.less";
import SearchCard, {SearhValues} from "./components/SearchCard";
import useSearchTable from "@/hooks/useSearchTable";
import {getFindReport} from "@/services/report";
import UseCacheForm from "@/hooks/useCacheForm";
import CardList from "./components/CardList";
import {Button} from "antd";
import CreateReportModal from "./createReportModal";
import {getCurrentUser} from "@/utils/authority";

const Report: React.FC = () => {
  const cacheForm = UseCacheForm();
  const user = getCurrentUser();

  const {tableProps, search} = useSearchTable(
    (params: any) => {
      return getFindReport(params);
    },
    cacheForm,
    {
      manual: true,
      defaultPageSize: 10
    }
  );

  const [createReportState, createReportDispatch] = useReducer(
    (state: any, action: any) => {
      return {
        ...state,
        ...action,
      };
    },
    {
      visible: false,
    }
  );
  const handleSearchValuesChange = (values?: SearhValues) => {
    cacheForm.setFieldsValue(values);
    search({
      current: 1,
      size: 5,
      zoneCode: user?.zoneCode,
    });
  };
  return (
    <>
      <div className={"report-create-wrapper"}>
        <Button
          className={"report-create"}
          type="primary"
          onClick={() => {
            createReportDispatch({
              visible: true,
            });
          }}
        >
          生成报告
        </Button>
      </div>
      <div className="report">
        <div className="content-left">
          <SearchCard
            onChange={handleSearchValuesChange}
            style={{minHeight: "220rem", marginBottom: "30rem"}}
          />
          <CardList
            {...tableProps}
            handleRefresh={(page: number) => {
              search({
                current: 1,
                size: 5,
                zoneCode: user?.zoneCode,
              });
            }}
            style={{minWidth: "1060rem"}}
          />
        </div>
      </div>
      <CreateReportModal
        {...createReportState}
        title="生成报告"
        dispatch={createReportDispatch}
        onSuccess={() => {
          search();
        }}
        onCancel={() => {
          createReportDispatch({
            visible: false,
          });
        }}
      />
    </>
  );
};

export default Report;
